package register;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Gjun
 */
@WebServlet(name = "registerservlet", urlPatterns = {"/RegisterPage/registerservlet"})
public class registerservlet extends HttpServlet {

    String username;
    String password;
    String identity1;
    String identity2;
    ResultSet rs;
    int count;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        username = request.getParameter("username").trim();
        password = request.getParameter("password").trim();

        if (password.length() == 0) {
            System.out.println("密碼未輸入");
            request.setAttribute("error", "密碼未輸入");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }
        if (username.length() == 0) {
            System.out.println("帳號未輸入");
            request.setAttribute("error", "帳號未輸入");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }

        if (username.length() > 10) {
            System.out.println("帳號太長");
            request.setAttribute("error", "帳號太長");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }
        if (password.length() > 10) {
            System.out.println("密碼太長");
            request.setAttribute("error", "密碼太長");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con
                    = DriverManager.getConnection("jdbc:sqlserver://114.32.112.40:168;databasename=NorthWind", "leagueofpg", "gjun");
            Statement st = con.createStatement();
            String sql = "Select 客戶編號 ,客戶密碼 from 客戶 where 客戶編號='" + username + "'";
            System.out.println(sql);
            rs = st.executeQuery(sql);

        } catch (Exception e) {
            System.out.println("帳號未輸入");
            request.setAttribute("error", "帳號未輸入");
            //response.sendRedirect("LoginPage/login.jsp");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }
        try {
            rs.next();
            count = rs.getRow();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (count == 0) {
            System.out.println("帳號不存在可註冊");
        } else {
            System.out.println("帳號存在");
            request.setAttribute("error", "帳號存在");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con
                    = DriverManager.getConnection("jdbc:sqlserver://114.32.112.40:168;databasename=NorthWind", "leagueofpg", "gjun");
            Statement st = con.createStatement();
            String sql = "insert into 客戶(客戶編號,客戶密碼,公司名稱)values('" + username + "','" + password + "','巨匠')";
            System.out.println(sql);
            st.executeUpdate(sql);

            request.getRequestDispatcher("/RegisterPage/Success/success.jsp").forward(request, response);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("資料輸入錯誤");
            request.setAttribute("error", "資料輸入錯誤");
            //response.sendRedirect("LoginPage/login.jsp");
            request.getRequestDispatcher("/RegisterPage/register.jsp").forward(request, response);
            return;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
